﻿using SeeSharpTools.JY.ArrayUtility;
using SeeSharpTools.JY.DSP.Fundamental;
using SeeSharpTools.JY.GUI;
using Sunny.UI;
using System;
using System.Collections.Generic;

namespace SeeSharpTools.Demo
{
    public partial class FormDigital : UIPage
    {
        private int signalCount;
        private List<EasyChartX> charts;
        private bool _internalOperation;

        public FormDigital()
        {
            InitializeComponent();

            uiComboBox.SelectedIndex = 0;
        }

        private void uiComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            int sampleCount = 512;
            signalCount = 4;

            byte[,] signal;

            switch (uiComboBox.SelectedIndex)
            {
                case 0:
                    signal = Generation.Digital.Gradient(sampleCount, signalCount);
                    break;

                case 1:
                    signal = Generation.Digital.Marching(sampleCount, signalCount);
                    break;

                case 2:
                    // 一共8个周期，每个周期空闲状态有4个点
                    signal = Generation.Digital.Periodic(sampleCount, signalCount, 8, 4);
                    break;

                case 3:
                    signal = Generation.Digital.Single(sampleCount, signalCount, DigitalState.High);
                    break;

                case 4:
                    signal = Generation.Digital.Random(sampleCount, signalCount);
                    break;

                case 5:
                    signal = Generation.Digital.Switch(sampleCount, signalCount);
                    break;

                default:
                    signal = Generation.Digital.Gradient(sampleCount, signalCount);
                    break;
            }

            Plot(signal);
        }

        private void easyChartX1_AxisViewChanged(object sender, EasyChartXViewEventArgs e)
        {
            if (_internalOperation) return;
            _internalOperation = true;

            for (int i = 1; i < charts.Count; i++)
            {
                charts[i % charts.Count].AxisX.Zoom(e.Axis.ViewMinimum + 1, e.Axis.ViewMaximum - 1);
            }

            _internalOperation = false;
        }

        private void easyChartX2_AxisViewChanged(object sender, EasyChartXViewEventArgs e)
        {
            if (_internalOperation) return;
            _internalOperation = true;

            for (int i = 2; i < charts.Count + 1; i++)
            {
                charts[i % charts.Count].AxisX.Zoom(e.Axis.ViewMinimum + 1, e.Axis.ViewMaximum - 1);
            }

            _internalOperation = false;
        }

        private void easyChartX3_AxisViewChanged(object sender, EasyChartXViewEventArgs e)
        {
            if (_internalOperation) return;
            _internalOperation = true;

            for (int i = 3; i < charts.Count + 2; i++)
            {
                charts[i % charts.Count].AxisX.Zoom(e.Axis.ViewMinimum + 1, e.Axis.ViewMaximum - 1);
            }

            _internalOperation = false;
        }

        private void easyChartX4_AxisViewChanged(object sender, EasyChartXViewEventArgs e)
        {
            if (_internalOperation) return;
            _internalOperation = true;

            for (int i = 4; i < charts.Count + 3; i++)
            {
                charts[i % charts.Count].AxisX.Zoom(e.Axis.ViewMinimum + 1, e.Axis.ViewMaximum - 1);
            }

            _internalOperation = false;
        }

        private void Plot(byte[,] signal)
        {
            byte[,] transposedSignal = new byte[signal.GetLength(1), signal.GetLength(0)];
            ArrayManipulation.Transpose(signal, ref transposedSignal);
            byte[] singleSignal = new byte[signal.GetLength(0)];

            charts = new List<EasyChartX>(signalCount)
            {
                easyChartX1,
                easyChartX2,
                easyChartX3,
                easyChartX4
            };
            for (int i = 0; i < charts.Count; i++)
            {
                charts[i].Clear();
            }

            for (int i = 0; i < signalCount; i++)
            {
                ArrayManipulation.GetArraySubset(transposedSignal, i, ref singleSignal, SeeSharpTools.JY.ArrayUtility.MajorOrder.Row);
                charts[i].Plot(singleSignal);
                charts[i].Series[0].Type = EasyChartXSeries.LineType.StepLine;
            }
        }
    }
}